package bar.dark.lee.setting.data;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import bar.dark.lee.BDConstents;
import bar.dark.lee.R;
import bar.dark.lee.SuperActivity;
import bar.dark.lee.common.BDUtil;
import bar.dark.lee.common.DBSQLite;
import bar.dark.lee.common.EditTextInputFilter;
import bar.dark.lee.setting.TipInfoActivity;
import bar.dark.lee.setting.TipsActivity;

public class DataAutoOutActivity extends SuperActivity
{
	private DBSQLite sql;
	
	private RadioButton okRadioButton;
	private RadioButton noRadioButton;
	private EditText fileCountEditText;
	private Button saveButton;
	
//	private RadioButton okMailRadioButton;
//	private RadioButton noMailRadioButton;
//	private EditText mailCountEditText;
//	private Button saveMailButton;
	
	private int fileFlag = 0;
	private int fileCount = 31;
	
//	private int mailFlag = 0;
//	private int mailCount = 3;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		sql = DBSQLite.getInstance(superContext);
		
		setContentView(R.layout.data_auto_out_activity);
		
		setTitle("자동 백업");
		
		setFindView();
		setOnClickListener();
		setData();
	}

	private void setFindView()
	{
		okRadioButton = (RadioButton) findViewById(R.id.dataAutoOutOkRadioButton);
		noRadioButton = (RadioButton) findViewById(R.id.dataAutoOutNoRadioButton);
		fileCountEditText = (EditText) findViewById(R.id.dataAutoOutFileCountEditText);
		saveButton = (Button) findViewById(R.id.dataAutoOutSaveButton);
		
//		okMailRadioButton = (RadioButton) findViewById(R.id.dataAutoMailOkRadioButton);
//		noMailRadioButton = (RadioButton) findViewById(R.id.dataAutoMailNoRadioButton);
//		mailCountEditText = (EditText) findViewById(R.id.dataAutoFileDateEditText);
//		EditTextInputFilter.filteringMoneyEditText(mailCountEditText);
//		saveMailButton = (Button) findViewById(R.id.dataAutoOutSaveButton);
	}
	
	private void setOnClickListener()
	{
//		((Button)findViewById(R.id.asdfasdfasdf)).setOnClickListener(new OnClickListener() {
//			@Override
//			public void onClick(View v) {
//				
//				
//				Intent i=new Intent(Intent.ACTION_SEND); 
//				//i.addCategory(Intent.CATEGORY_DEFAULT); 
//				    	
//				// for EMail
//				// mimetype
//				//i.putExtra(android.content.Intent.ACTION_SENDTO, new String[]{"hyoijuni@gmail.com"}); 
//				i.setType("text/plain");
//				i.putExtra(Intent.EXTRA_SUBJECT, "제목입니다."); 
//				i.putExtra(Intent.EXTRA_TEXT, "본문이구요 ....");
//				i.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"hyoijuni@gmail.com"}); 
//				//i.putExtra(Intent.EXTRA_CC, new String[]{"user_cc1@website.com", "user_cc2@website.com"}); 
//				//i.putExtra(Intent.EXTRA_BCC, new String[]{"user_bcc1@website.com"}); 
//				// 파일 첨부
//				i.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/AccountBook/메일받음.db"));
//				// 앱선택 박스를 띄움.
//				//startActivity(Intent.createChooser(i, "How do you want to send message?"));
//				//i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//				
//				// com.google.android.gm 패키지의 ComposeActivityGmail 액티비티를 명시적으로 기입해도 될듯~!
//				//i.setComponent(new ComponentName("com.google.android.gm","com.google.android.gm.ComposeActivity"));
//				//startActivity(i);
//				startActivity(Intent.createChooser(i, "E-mail"));
//
//				
//				
//				Mail m = new Mail("hyoijuni@gmail.com", "dlwmal51");
//				
//				String[] toArr = {"hyoijuni@gmail.com"}; // This is an array, you can add more emails, just separate them with a coma
//				m.setTo(toArr); // load array to setTo function
//				m.setFrom("hyoijuni@gmail.com"); // who is sending the email
//				m.setSubject("subject");
//				m.setBody("your message goes here"); 
//		 
//				try {
//					m.addAttachment(Environment.getExternalStorageDirectory().getPath() + "/AccountBook/메일받음.db");  // path to file you want to attach
//					if(m.send()) {
//						// success
//						Toast.makeText(DataAutoOutActivity.this, "Email was sent successfully.", Toast.LENGTH_LONG).show();
//					} else {
//						// failure
//						Toast.makeText(DataAutoOutActivity.this, "Email was not sent.", Toast.LENGTH_LONG).show();
//					}
//				} catch(Exception e) {
//					// some other problem
//					Toast.makeText(DataAutoOutActivity.this, "There was a problem sending the email.", Toast.LENGTH_LONG).show();
//				} 
//			}
//		});
		
		okRadioButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				okRadioButton.setChecked(true);
				noRadioButton.setChecked(false);
			}
		});
		
		noRadioButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				okRadioButton.setChecked(false);
				noRadioButton.setChecked(true);
			}
		});
		
//		okMailRadioButton.setOnClickListener(new OnClickListener()
//		{
//			@Override
//			public void onClick(View v)
//			{
//				okMailRadioButton.setChecked(true);
//				noMailRadioButton.setChecked(false);
//			}
//		});
//		
//		noMailRadioButton.setOnClickListener(new OnClickListener()
//		{
//			@Override
//			public void onClick(View v)
//			{
//				okMailRadioButton.setChecked(false);
//				noMailRadioButton.setChecked(true);
//			}
//		});
		
		saveButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				SharedPreferences pref = getSharedPreferences("pref", Activity.MODE_PRIVATE);
				SharedPreferences.Editor editor = pref.edit();
				
				if (okRadioButton.isChecked())
				{
					editor.putInt(BDConstents.DATA_AUTO_OUT_SETTING, 0);
				}
				else
				{
					editor.putInt(BDConstents.DATA_AUTO_OUT_SETTING, 1);
				}
				
				int count = Integer.parseInt(fileCountEditText.getText().toString());
				
				editor.putInt(BDConstents.DATA_AUTO_OUT_FILE_COUNT, count);
				
				editor.commit();
				
				BDUtil.showDialogFinish(superContext, "알림", "오토 백업 정보를 저장 하였습니다.");
			}
		});
		
//		saveMailButton.setOnClickListener(new OnClickListener()
//		{
//			@Override
//			public void onClick(View v)
//			{
//				SharedPreferences pref = getSharedPreferences("pref", Activity.MODE_PRIVATE);
//				SharedPreferences.Editor editor = pref.edit();
//				
//				if (okMailRadioButton.isChecked())
//				{
//					editor.putInt(BDConstents.DATA_AUTO_MAIL_SETTING, 0);
//				}
//				else
//				{
//					editor.putInt(BDConstents.DATA_AUTO_MAIL_SETTING, 1);
//				}
//				
//				int count = Integer.parseInt(mailCountEditText.getText().toString());
//				
//				editor.putInt(BDConstents.DATA_AUTO_OUT_MAIL_COUNT, count);
//				
//				editor.commit();
//				
//				BDUtil.showDialogFinish(superContext, "알림", "오토 백업 정보를 저장 하였습니다.");
//			}
//		});
	}
	
	private void setData()
	{
		SharedPreferences pref = getSharedPreferences("pref", Activity.MODE_PRIVATE);
		fileCount = pref.getInt(BDConstents.DATA_AUTO_OUT_FILE_COUNT, 31);
		fileFlag = pref.getInt(BDConstents.DATA_AUTO_OUT_SETTING, 0);
		
		if (fileFlag == 0)
		{
			okRadioButton.setChecked(true);
			noRadioButton.setChecked(false);
		}
		else
		{
			okRadioButton.setChecked(false);
			noRadioButton.setChecked(true);
		}
		
		fileCountEditText.setText(fileCount + "");
		
//		mailCount = pref.getInt(BDConstents.DATA_AUTO_OUT_MAIL_COUNT, 3);
//		mailFlag = pref.getInt(BDConstents.DATA_AUTO_MAIL_SETTING, 0);
//		
//		if (mailFlag == 0)
//		{
//			okMailRadioButton.setChecked(true);
//			noMailRadioButton.setChecked(false);
//		}
//		else
//		{
//			okMailRadioButton.setChecked(false);
//			noMailRadioButton.setChecked(true);
//		}
//		
//		mailCountEditText.setText(mailCount + "");
	}

	@Override
	protected void tipButton()
	{
		Intent intent = new Intent(this, TipInfoActivity.class);
		intent.putExtra(TipsActivity.TIP_REQUEST_KEY, TipsActivity.TIP_DATABANKUP);
		startActivity(intent);
	}
}
